package com.syg.logback.core;

import com.syg.boot.util.net.NetUtil;
import com.syg.boot.util.text.StringUtil;
import com.syg.common.context.SygRootContext;
import com.syg.logback.consts.LogTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * @author syg
 * @version 1.0
 * @date 2021年11月24日19:14:46
 */
public class BizTranInf implements Serializable {
    private static final Logger log = LoggerFactory.getLogger(BizTranInf.class);
    private static final long serialVersionUID = 1L;
    private String flowId;
    private String bizId;
    private String tranCd;
    private String idNo;
    private String name;
    private String channelNo;
    private String ip;
    private String sendTime;
    private String receiveTime;
    private String requestData;
    private String responseData;
    private String respCode;
    private String exeSts;
    private String grayscaleFlag;

    @Override
    public String toString() {
        return LogTypeEnum.BIZTRAN + "@@@" + this.flowId + "#@%@#" + this.bizId + "#@%@#" + this.tranCd + "#@%@#" + this.idNo + "#@%@#" + this.name + "#@%@#" + this.channelNo + "#@%@#" + this.ip + "#@%@#" + this.sendTime + "#@%@#" + this.receiveTime + "#@%@#" + this.requestData + "#@%@#" + this.responseData + "#@%@#" + this.exeSts + "#@%@#" + this.respCode + "#@%@#" + this.grayscaleFlag;
    }

    public BizTranInf(BizTranBuilder builder) {
        this.flowId = builder.getFlowId();
        this.bizId = builder.getBizId();
        this.tranCd = builder.getTranCd();
        this.idNo = builder.getIdNo();
        this.name = builder.getName();
        this.channelNo = builder.getChannelNo();
        this.ip = NetUtil.getLocalHost();
        this.sendTime = builder.getSendTime();
        this.receiveTime = builder.getReceiveTime();
        this.requestData = builder.getRequestData();
        this.responseData = builder.getResponseData();
        this.exeSts = builder.getExeSts();
        this.respCode = builder.getRespCode();
        this.dealGrayscale();
    }

    public BizTranInf(String flowId, String bizId, String tranCd, String idNo, String name, String channelNo, String sendTime, String receiveTime, String requestData, String responseData, String exeSts) {
        this.flowId = flowId;
        this.bizId = bizId;
        this.tranCd = tranCd;
        this.idNo = idNo;
        this.name = name;
        this.channelNo = channelNo;
        this.ip = NetUtil.getLocalHost();
        this.sendTime = sendTime;
        this.receiveTime = receiveTime;
        this.requestData = requestData;
        this.responseData = responseData;
        this.exeSts = exeSts;
        this.dealGrayscale();
    }

    public void setException(Throwable e) {
        String excpetionMsg = e.getMessage();
        if (StringUtil.isEmptyAll(excpetionMsg)) {
            this.respCode = "Z999999";
        } else {
            String[] array = excpetionMsg.split("##@##");
            if (array.length == 1) {
                String msg = array[0];
                int end = Math.min(msg.length(), 20);
                this.respCode = msg.substring(0, end);
            } else if (array.length >= 2) {
                this.respCode = array[0];
            }

        }
    }

    private void dealGrayscale() {
        try {
            String str = SygRootContext.get("grayscaleFlag");
            if ("true".equals(str)) {
                this.grayscaleFlag = "1";
            } else {
                this.grayscaleFlag = "0";
            }

            if (StringUtils.isNotEmpty(str)) {
                SygRootContext.remove("grayscaleFlag");
            }
        } catch (Exception var2) {
            log.error("dealGrayscale-Exception:", var2);
        }

    }

    public void setFlowId(String flowId) {
        this.flowId = flowId;
    }

    public void setBizId(String bizId) {
        this.bizId = bizId;
    }

    public void setTranCd(String tranCd) {
        this.tranCd = tranCd;
    }

    public void setIdNo(String idNo) {
        this.idNo = idNo;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setChannelNo(String channelNo) {
        this.channelNo = channelNo;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public void setSendTime(String sendTime) {
        this.sendTime = sendTime;
    }

    public void setReceiveTime(String receiveTime) {
        this.receiveTime = receiveTime;
    }

    public void setRequestData(String requestData) {
        this.requestData = requestData;
    }

    public void setResponseData(String responseData) {
        this.responseData = responseData;
    }

    public void setRespCode(String respCode) {
        this.respCode = respCode;
    }

    public void setExeSts(String exeSts) {
        this.exeSts = exeSts;
    }

    public void setGrayscaleFlag(String grayscaleFlag) {
        this.grayscaleFlag = grayscaleFlag;
    }

    public String getFlowId() {
        return this.flowId;
    }

    public String getBizId() {
        return this.bizId;
    }

    public String getTranCd() {
        return this.tranCd;
    }

    public String getIdNo() {
        return this.idNo;
    }

    public String getName() {
        return this.name;
    }

    public String getChannelNo() {
        return this.channelNo;
    }

    public String getIp() {
        return this.ip;
    }

    public String getSendTime() {
        return this.sendTime;
    }

    public String getReceiveTime() {
        return this.receiveTime;
    }

    public String getRequestData() {
        return this.requestData;
    }

    public String getResponseData() {
        return this.responseData;
    }

    public String getRespCode() {
        return this.respCode;
    }

    public String getExeSts() {
        return this.exeSts;
    }

    public String getGrayscaleFlag() {
        return this.grayscaleFlag;
    }
}
